package com.kcsj.controller.basic;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kcsj.domain.ProductModel;
import com.kcsj.domain.Warehouse;
import com.kcsj.domain.WarehouseInputRecord;
import com.kcsj.domain.WarehouseOutRecord;
import com.kcsj.service.WarehouseInputRecordService;
import com.kcsj.service.WarehouseOutRecordService;
import com.kcsj.util.Condition;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import com.mysql.cj.xdevapi.JsonArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

@WebServlet("rfid.ctl")
public class RFIDController extends HttpServlet {

    private WarehouseInputRecordService warehouseInputRecordService = WarehouseInputRecordService.getInstance();
    private WarehouseOutRecordService warehouseOutRecordService = WarehouseOutRecordService.getInstance();
    //增加单条入库记录
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
            JSONObject req_jsonObj = JSONObject.parseObject(JSONUtil.getJSON(request));
            String tagInformation = req_jsonObj.getString("tagInformation");
            String productModelNo = req_jsonObj.getString("productModelNo");
            String warehouseNo = req_jsonObj.getString("warehouseNo");
            Warehouse warehouse = new Warehouse();
            warehouse.setWarehouseNo(warehouseNo);
            WarehouseInputRecord warehouseInputRecord = new WarehouseInputRecord();
            warehouseInputRecord.setTagInformation(tagInformation);
            ProductModel productModel = new ProductModel();
            productModel.setWarehouse(warehouse);
            productModel.setProductModelNo(productModelNo);
            warehouseInputRecord.setProductModel(productModel);
            warehouseInputRecordService.add(warehouseInputRecord,1);
            response.getWriter().println(MessageHelper.addMessage("success"));
        }catch (SQLException e){
            response.getWriter().println(MessageHelper.dateBaseError());
            e.printStackTrace();
        }catch (Exception e){
            response.getWriter().println(MessageHelper.error());

        }
    }

    //请求增加出库记录
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
            JSONObject req_jsonObj = JSONObject.parseObject(JSONUtil.getJSON(request));
            String tagInformation = req_jsonObj.getString("tagInformation");
            String productModelNo = req_jsonObj.getString("productModelNo");
            String warehouseNo = req_jsonObj.getString("warehouseNo");
            Warehouse warehouse = new Warehouse();
            warehouse.setWarehouseNo(warehouseNo);
            WarehouseOutRecord warehouseOutRecord = new WarehouseOutRecord();
            warehouseOutRecord.setTagInformation(tagInformation);
            ProductModel productModel = new ProductModel();
            productModel.setWarehouse(warehouse);
            productModel.setProductModelNo(productModelNo);
            warehouseOutRecord.setProductModel(productModel);
            warehouseOutRecordService.add(warehouseOutRecord,1);
            response.getWriter().println(MessageHelper.addMessage("success"));
        }catch (SQLException e){
            response.getWriter().println(MessageHelper.dateBaseError());
            e.printStackTrace();
        }catch (Exception e){
            response.getWriter().println(MessageHelper.error());
            
        }
    }
}
